package main

import (
	"context"
	"fmt"
	"net"
	"newtomicroc/consulconn/consul/api"
	"newtomicroc/consulconn/pb"
	"newtomicroc/rpcconn/pb/grpc-go"
)

type Children struct {
}

func (this *Children) SayHello(ctx context.Context, p *pb.Person) (*pb.Person, error) {
	p.Name = "hello" + p.Name
	return p, nil
}

func main() {

	//初始化consule配置
	consulConfig := api.DefaultConfig()
	//2 创建consul对象
	consuleClient, err := api.NewClient(consulConfig)
	if err != nil {
		fmt.Println("api.NewClient err：", err)
		return
	}
	//3 通知consul 服务要注册的信息

	reg := api.AgentServiceRegistration{
		ID:      "bj38",
		Tags:    []string{"grpc", "consul"},
		Name:    "grpc and consul",
		Address: "127.0.0.1",
		Check: &api.AgentServiceCheck{
			CheckID:  "consul grpc test",
			TCP:      "127.0.0.1:8800",
			Timeout:  "1s",
			Interval: "5s",
		},
	}

	//4注册grpc服务到consul 上
	consuleClient.Agent().ServiceRegister()

	//《《《《《《《《《《《《《
	//初始化grpc对象
	grpcServer := grpc.NewServer()
	//2注册服务
	pb.RegisterHelloServer(grpcServer, new(Children))

	//3设置监听 指定ip/port
	listener, err := net.Listen("tcp", "127.0.0.1:8800")
	if err != nil {
		fmt.Println("listener err：", err)
		return
	}
	defer listener.Close()
	//4启动服务
	grpcServer.Serve(listener)
}
